﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Media.Imaging;
using System.Threading.Tasks;

namespace VideoClient.Utils
{
   static class ImageColorConverter
    {
        //public static BitmapSource YCbCrToRGB(NTheora.YCbCrImage encodedImage)
        //{
        //    int bytes = encodedImage.Stride * encodedImage.Height;
        //    int pixels = encodedImage.Width * encodedImage.Height;
        //    var bitmapData = new byte[bytes];

        //    Parallel.For(0, pixels - 1, (i) =>
        //    {
        //        double y = encodedImage.Y.Data[i];
        //        double cb = encodedImage.Cb.Data[i] - 128;
        //        double cr = encodedImage.Cr.Data[i] - 128;

        //        bitmapData[i * 3] = (byte)(y + 1.4022 * cr);//r
        //        bitmapData[i * 3 + 1] = (byte)Math.Min(255, Math.Max(0, (y - 0.3456 * cb - 0.7145 * cr)));//g
        //        bitmapData[i * 3 + 2] = (byte)Math.Min(255, Math.Max(0, (y + 1.7710 * cb)));//b
        //    });

        //    //for (int i = 0, pixel = 0; i < bytes; pixel++)
        //    //{
        //    //    double y = encodedImage.Y.Data[pixel];

        //    //    double cb = encodedImage.Cb.Data[pixel] - 128;
        //    //    double cr = encodedImage.Cr.Data[pixel] - 128;
        //    //    bitmapData[i++] = (byte)(y + 1.4022 * cr);//r
        //    //    bitmapData[i++] = (byte)Math.Min(255, Math.Max(0, (y - 0.3456 * cb - 0.7145 * cr)));//g
        //    //    bitmapData[i++] = (byte)Math.Min(255, Math.Max(0, (y + 1.7710 * cb )));//b

        //    //    //double cb = cbData.Data[pixel];
        //    //    //double cr = crData.Data[pixel];
        //    //    //bitmapData[i++] = (byte)((298.082 * y + 408.583 * cr) / 256.0 - 222.921);//r
        //    //    //bitmapData[i++] = (byte)((298.082 * y - 100.291 * cb - 208.120 * cr) / 256.0 + 135.576);//g
        //    //    //bitmapData[i++] = (byte)((298.082 * y + 516.412 * cb) / 256.0 - 276.836);//b
        //    //}

        //    return BitmapImage.Create(encodedImage.Width, encodedImage.Height, 96, 96, System.Windows.Media.PixelFormats.Rgb24, BitmapPalettes.BlackAndWhite, bitmapData, encodedImage.Stride);
        //}

        //public static NTheora.YCbCrImage RGBToYCbCr(this Bitmap rgbImage)
        //{
        //    rgbImage.RotateFlip(RotateFlipType.RotateNoneFlipY);

        //    if (rgbImage.PixelFormat != PixelFormat.Format24bppRgb)
        //        throw new InvalidOperationException();

        //    // coefficients taken from the color space faq
        //    /*
        //      RGB -> YCbCr (with Rec 601-1 specs)
        //      Y  =  0.2989 * Red + 0.5866 * Green + 0.1145 * Blue
        //      Cb = -0.1687 * Red - 0.3312 * Green + 0.5000 * Blue
        //      Cr =  0.5000 * Red - 0.4183 * Green - 0.0816 * Blue

        //      YCbCr (with Rec 601-1 specs) -> RGB
        //      Red   = Y + 0.0000 * Cb + 1.4022 * Cr
        //      Green = Y - 0.3456 * Cb - 0.7145 * Cr
        //      Blue  = Y + 1.7710 * Cb + 0.0000 * Cr
        //    */

        //    var data = rgbImage.LockBits(new Rectangle(Point.Empty, rgbImage.Size), ImageLockMode.ReadOnly, rgbImage.PixelFormat);

        //    int bytes = data.Stride * rgbImage.Height;
        //    int pixels = rgbImage.Width * rgbImage.Height;
        //    byte[] rgbData = new byte[bytes];
        //    Marshal.Copy(data.Scan0, rgbData, 0, bytes);
        //    rgbImage.UnlockBits(data);

        //    var result = new NTheora.YCbCrImage (rgbImage.Width, rgbImage.Height);

        //    int counter = 0;
        //    Parallel.For(counter, pixels - 1, (i) =>
        //    {
        //        double r = rgbData[i * 3];
        //        double g = rgbData[i * 3 + 1];
        //        double b = rgbData[i * 3 + 2];

        //        result.Y.Data[i] = (byte)((0.2989 * r + 0.5866 * g + 0.1145 * b));
        //        result.Cb.Data[i] = (byte)((0.5000 * r - 0.4184 * g - 0.0816 * b) + 128);
        //        result.Cr.Data[i] = (byte)((-0.1687 * r - 0.3313 * g + 0.5000 * b) + 128);
        //    });

        //    return result;
        //}
    }
}
